#!/usr/bin/env python
# @Author:      The Grugq (grugq@hcunix.net)

import headerbases
import headerutils

class Accept(headerbases.Mime): pass
class AcceptEncoding(headerbases.Token): pass
class AcceptLanguage(headerbases.Token): pass
class AlertInfo(headerbases.GenericURI): pass
class Allow(headerbases.Token): pass
class AuthenticationInfo(headerbases.Auth): pass
class Authorization(headerbases.Auth): pass
class CallID(headerbases.CallID): pass
class CallInfo(headerbases.GenericURI): pass
class Contact(headerbases.NameAddr): pass
class ContentDisposition(headerbases.Token): pass
class ContentEncoding(headerbases.Token): pass
class ContentLanguage(headerbases.Token): pass
class ContentLength(headerbases.Int): pass
class ContentType(headerbases.Mime): pass
class ContentTransferEncoding(headerbases.Mime): pass
class CSeq(headerbases.CSeq): pass
class Date(headerbases.Date): pass
class ErrorInfo(headerbases.GenericURI): pass
class Expires(headerbases.Int): pass
class From(headerbases.NameAddr): pass
class InReplyTo(headerbases.CallID): pass
class MaxForwards(headerbases.Int): pass
class MIMEVersion(headerbases.Token): pass
class MinExpires(headerbases.Int): pass
class Organization(headerbases.String): pass
class Priority(headerbases.Token): pass
class ProxyAuthenticate(headerbases.Auth): pass
class ProxyAuthorization(headerbases.Auth): pass
class ProxyRequire(headerbases.Token): pass
class RecordRoute(headerbases.NameAddr): pass
class ReplyTo(headerbases.NameAddr): pass
class Require(headerbases.Token): pass
class RetryAfter(headerbases.Int): pass
class Route(headerbases.NameAddr): pass
class Server(headerbases.String): pass
class Subject(headerbases.String): pass
class Supported(headerbases.Token): pass
class Timestamp(headerbases.String): pass
class To(headerbases.NameAddr): pass
class Unsupported(headerbases.Token): pass
class UserAgent(headerbases.String): pass
class Via(headerbases.Via): pass
class Warning(headerbases.Warning): pass
class WWWAuthenticate(headerbases.Auth): pass
#
class GenericHeader(headerbases.Token): pass

_FactoryDict = {
        "Accept" : Accept,
        "Accept-Encoding" : AcceptEncoding,
        "Accept-Language" : AcceptLanguage,
        "Alert-Info" : AlertInfo,
        "Allow" : Allow,
        "Authentication-Info" : AuthenticationInfo,
        "Authorization" : Authorization,
        "Call-ID" : CallID,
        "Call-Info" : CallInfo,
        "Contact" : Contact,
        "Content-Disposition" : ContentDisposition,
        "Content-Encoding" : ContentEncoding,
        "Content-Language" : ContentLanguage,
        "Content-Length" : ContentLength,
        "Content-Type" : ContentType,
        "CSeq" : CSeq,
        "Date" : Date,
        "Error-Info" : ErrorInfo,
        "Expires" : Expires,
        "From" : From,
        "In-Reply-To" : InReplyTo,
        "Max-Forwards" : MaxForwards,
        "MIME-Version" : MIMEVersion,
        "Min-Expires" : MinExpires,
        "Organization" : Organization,
        "Priority" : Priority,
        "Proxy-Authenticate" : ProxyAuthenticate,
        "Proxy-Authorization" : ProxyAuthorization,
        "Proxy-Require" : ProxyRequire,
        "Record-Route" : RecordRoute,
        "Reply-To" : ReplyTo,
        "Require" : Require,
        "Retry-After" : RetryAfter,
        "Route" : Route,
        "Server" : Server,
        "Subject" : Subject,
        "Supported" : Supported,
        "Timestamp" : Timestamp,
        "To" : To,
        "Unsupported" : Unsupported,
        "User-Agent" : UserAgent,
        "Via" : Via,
        "Warning" : Warning,
        "WWW-Authenticate" : WWWAuthenticate,
    }

# XXX special parsing for kwargs
#       treat kwarg as params for the primary value
#       if len(args) == 1: args[0] is a buffer
#       else:
#           args[0] == header_name, args[1:] == values

# e.g. like so:
# msg += headers.Header("to", "joe blogs <f@balh.com>", p="qqq", v="yaya")

def Header(*args, **kwargs):
    if args:
        if len(args) == 1: # args is a buf
            buf = args[0]
            hname = headerutils.normalize( buf.split(":", 1)[0] )
        else:
            hname = headerutils.normalize( args[0] )
            buf = args[1]
        if hname in _FactoryDict:
            hdr = _FactoryDict[hname]
        else:
            hdr = GenericHeader
    else:
        buf = ''
        hdr = GenericHeader

    return hdr(buf, params=kwargs)
